﻿using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
//using System.Drawing;
//using System.Text;
//using System.Xml;
using System.Windows.Forms;
using Models;
using BLL;
using System.IO;
using System.Net;
using System.Text;
using System.Diagnostics;

namespace LinSM
{
    /// <summary>
    /// 登陆窗体
    /// </summary>
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        //隐藏标题后可以移动窗体
        internal static int WM_NCHITTEST = 0x84;
        internal static IntPtr HTCLIENT = (IntPtr)0x1;
        internal static IntPtr HTCAPTION = (IntPtr)0x2;
        internal static int WM_NCLBUTTONDBLCLK = 0x00A3;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCLBUTTONDBLCLK)
            {
                return;
            }
            if (m.Msg == WM_NCHITTEST)
            {
                base.WndProc(ref m);
                if (m.Result == HTCLIENT)
                {
                    m.HWnd = this.Handle;
                    m.Result = HTCAPTION;
                }
                return;
            }
            base.WndProc(ref m);
        }
        //隐藏标题后可以移动窗体　end
        LoginManager lm = new LoginManager();
        //登陆
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(version()) == 2)
            {
                MessageBox.Show("当前版本已经不可用！");
                return;
            }
            string id = txtId.Text.Trim();
            string pwd = txtPwd.Text.Trim();

            if (id == "")
            {
                MessageBox.Show("请输入帐号！");
                txtId.Select();
                return;
            }
            if (pwd == "")
            {
                MessageBox.Show("请输入密码！");
                txtPwd.Select();
                return;
            }
            if (lm.encode(id, pwd))
            {
                if (!LoadConfig())
                {
                    return;
                }

                //MessageBox.Show("登陆成功");
                Main f2 = new Main();
                Close();
                f2.Show();
            }
            else
            {
                MessageBox.Show("用户名或密码错误,或者你没有登录权限！");
            }
            
        }

        private bool LoadConfig()
        {
            if (Config.checkVer)
            {
                if (Config.verType != 2)
                {
                    return false;
                }
                if (!lm.isVer())
                {
                    if (!Config.checkVerLogin)
                    {
                        MessageBox.Show("检测到LinSM v3与数据库并不匹配\n继续操作可能会导致数据库混乱！\nLinSM v3 目前不允许你继续操作");
                        return false;
                    }
                    else
                    {
                        MessageBox.Show("检测到LinSM v3与数据库并不匹配\n继续操作可能会导致数据库混乱！\n");
                    }
                }
            }
            return true;
        }
        private void Login_Load(object sender, EventArgs e)
        {
            System.Diagnostics.Process.GetCurrentProcess().MaxWorkingSet = (IntPtr)750000;
            try
            {
                if (Config.checkLvl)
                {
                    Register.GetLevel();
                }
            }
            catch
            {
                Link a = new Link();
                Close();
                a.Show();
            }

            //UpDate up = new UpDate();
            //up.isUp();
            
        }

        private void lblClose_Click(object sender, EventArgs e)
        {
            Application.Exit();//退出
        }

        

        private void lblCon_Click(object sender, EventArgs e)
        {
            Link a = new Link();
            Close();
            a.Show();
        }

        private string version()
        {
            try
            {
                string strUrl = "http://www.5xiao.com/linsm/1.txt";
                Uri uri = new Uri(strUrl);
                WebRequest wr = WebRequest.Create(uri);
                Stream s = wr.GetResponse().GetResponseStream();
                StreamReader sr = new StreamReader(s, Encoding.Default);
                string all = sr.ReadToEnd();
                int i = all.IndexOf("[") + 2;//
                string tempver = all.Substring(i, 1);
                string ver = tempver.Replace("]", "").Replace(" ", "");
                return ver;
            }
            catch
            {
                return Convert.ToString(2);
            }
        }

        private void txtZhu_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("accounts表access_level设置为100，该帐号即可从LinSM登陆");
        }

        private void lblTTSF_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("iexplore.exe", "http://www.ttsf.net");
        }


    }
}
